home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 12 / CU Amiga Magazine's Super CD-ROM 12 (1997)(EMAP Images)(GB)[!][issue 1997-07].iso / CUCD / Games / DestructivePoker / sources / sources.lha / cstring.h < prev    next >
C/C++ Source or Header  |  1997-02-20  |  3KB  |  102 lines

  1. /*
  2.         cstring.h
  3.  
  4.         V2.00 - 180297  Kimmo Teräväinen
  5.         -----   ------  ----------------
  6.         V1.01   180297  Started to create 2nd release of string class.
  7.                         Reason for this is failures to link GNU String
  8.                         with libnix.
  9.         V1.10   190297  Tested shortly. Seem to work.
  10.         V1.11   190297  Added lastchar().
  11.         V1.12   190297  Added +=(const char) & =(const char)
  12.         V1.13   190297  Added ==
  13. */
  14. #ifndef ACL_CSTRING
  15. #define ACL_CSTRING
  16.  
  17. #include <exec/types.h>
  18.  
  19. class cstring {
  20.   char *data;
  21.   int size;
  22. protected:
  23.   void cstrcopy(const char*,char*,int);
  24.   int cstrlength(const char*) const;
  25.  
  26.   void Alloc(int Size) {
  27.     Delete();
  28.     if(Size>0) {
  29.       data=new char[Size];
  30.       if(data) {
  31.         size=Size;
  32.         data[0]=0;
  33.       }
  34.     }
  35.   }
  36.   void ReAlloc(int Size) {
  37.     if(Size>0) {
  38.       char *t=new char[Size];
  39.       if(t) {
  40.         cstrcopy(data,t,Size);
  41.         Delete();
  42.         data=t; size=Size;
  43.       }
  44.     }
  45.   }
  46.   void Delete() { if(data) delete [] data; data=NULL; size=0; }
  47. public:
  48.   cstring(const char *text=NULL) : data(NULL), size(0) { *this=text; }
  49.   ~cstring() { Delete(); }
  50.  
  51.   clear() { if(data) data[0]=0; }
  52.  
  53.   const char *chars() const { return data; }
  54.   int length() const { return cstrlength(data); }
  55.   char lastchar() const;
  56.  
  57.   operator const char*() const { return data; }
  58.   operator UBYTE*() const { return (UBYTE*)data; } // for AmigaOS
  59.   operator int() const { return length(); }
  60.  
  61.   cstring &operator=(const char *text) {
  62.     int l=1;
  63.     if(text) l+=cstrlength(text);
  64.     Alloc(l);
  65.     cstrcopy(text,data,size);
  66.     return *this;
  67.   }
  68.   cstring &operator=(const cstring &s) { return *this=s.data; }
  69.   cstring &operator=(const UBYTE *txt) { return *this=(const char*)txt; }
  70.   cstring &operator=(const char c) {
  71.     char cc[2];
  72.     cc[0]=c; cc[1]=0;
  73.     return *this=cc;
  74.   }
  75.  
  76.   cstring &operator+=(const char *text) {
  77.     if(text) {
  78.       int l=length();
  79.       ReAlloc(l+cstrlength(text)+1);
  80.       cstrcopy(text,&data[l],size-l);
  81.     }
  82.     return *this;
  83.   }
  84.   cstring &operator+=(const cstring &s) { return *this+=s.data; }
  85.   cstring &operator+=(const UBYTE *txt) { return *this+=(const char*)txt; }
  86.   cstring &operator+=(const char c) {
  87.     char cc[2];
  88.     cc[0]=c; cc[1]=0;
  89.     return *this+=cc;
  90.   }
  91.  
  92.   int operator==(const char *text) const;
  93.   int operator==(const cstring &s) const { return *this==s.data; }
  94.   int operator==(const UBYTE *txt) const { return *this==(const char*)txt; }
  95.   int operator==(const char c) {
  96.     if(length()!=1) return FALSE;
  97.     return data[0]==c;
  98.   }
  99. };
  100.  
  101. #endif
  102.